#!/bin/bash

# =============== 配置区 ===============
NDK_DIR="/home/vm/android-ndk-r21d"
ANDROID_API=26
ARCH="armeabi-v7a"
PREFIX="$(pwd)/android/${ARCH}"
X264_PREFIX="/home/vm/GB28181/x264/android/armeabi-v7a"

# =============== 工具链路径 ===============
TOOLCHAIN_DIR="$NDK_DIR/toolchains/llvm/prebuilt/linux-x86_64"
CC="$TOOLCHAIN_DIR/bin/armv7a-linux-androideabi${ANDROID_API}-clang"
CXX="$TOOLCHAIN_DIR/bin/armv7a-linux-androideabi${ANDROID_API}-clang++"
AR="$TOOLCHAIN_DIR/bin/llvm-ar"
STRIP="$TOOLCHAIN_DIR/bin/llvm-strip"
NM="$TOOLCHAIN_DIR/bin/llvm-nm"
SYSROOT="$NDK_DIR/toolchains/llvm/prebuilt/linux-x86_64/sysroot"

# =============== 检查编译器 ===============
echo "🔍 正在检查编译器..."
if [ ! -f "$CC" ]; then
    echo "❌ 错误：找不到编译器 $CC"
    exit 1
fi
echo "✅ 编译器存在：$CC"

echo "🔍 正在检查 x264 头文件..."
if [ ! -f "$X264_PREFIX/include/x264.h" ]; then
    echo "❌ 错误：找不到 x264.h 头文件，请确认 x264 安装路径正确"
    exit 1
fi
echo "✅ x264 头文件检查通过"

# =============== 创建输出目录 ===============
mkdir -p "$PREFIX"
echo "📁 输出目录：$PREFIX"

# =============== 清理旧构建 ===============
echo "🧹 正在清理旧文件..."
make distclean > /dev/null 2>&1 || true
rm -f config.log

# =============== 配置 FFmpeg ===============
echo "⚙️ 正在配置 FFmpeg..."

./configure \
    --prefix="$PREFIX" \
    --arch=arm \
    --cpu=cortex-a8 \
    --target-os=android \
    --enable-cross-compile \
    --cc="$CC" \
    --cxx="$CXX" \
    --ar="$AR" \
    --strip="$STRIP" \
    --nm="$NM" \
    --enable-pic \
    --enable-shared \
    --disable-static \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-avdevice \
    --disable-postproc \
    --disable-symver \
    --disable-stripping \
    \
    --enable-gpl \
    --enable-libx264 \
    \
    --extra-cflags="-I$X264_PREFIX/include -DANDROID -O3 -fPIC" \
    --extra-ldflags="-L$X264_PREFIX/lib -Wl,--allow-multiple-definition" \
    --extra-libs="-lx264 -lm -lz" \
    \
    --sysroot="$SYSROOT" > config.log 2>&1

# =============== 检查配置是否成功 ===============
if [ $? -ne 0 ]; then
    echo "❌ 配置失败！运行下面命令查看错误："
    echo "cat config.log | grep -i error"
    exit 1
fi

# 检查是否启用 x264 和 muxers
echo "🔍 检查是否启用 libx264 和 muxers..."
grep "libx264.*enabled" config.log
grep "Muxers.*enabled" config.log

echo "✅ 配置成功！"

# =============== 编译 ===============
echo "🛠 正在编译 FFmpeg...（可能需要 10-20 分钟）"
make -j$(nproc)

if [ $? -ne 0 ]; then
    echo "❌ 编译失败！"
    exit 1
fi

# =============== 安装 ===============
echo "📦 正在安装..."
make install

if [ $? -ne 0 ]; then
    echo "❌ 安装失败！"
    exit 1
fi

# =============== 验证结果 ===============
if [ -f "$PREFIX/lib/libavcodec.so" ]; then
    echo "🎉 🎉 🎉 编译成功！🎉 🎉 🎉"
    echo "生成的动态库："
    echo ""
    ls -lh "$PREFIX/lib/" | grep ".so"
    echo ""
    echo "📌 提示："
    echo "   - 已启用 libx264 (H.264) 编码支持"
    echo "   - 音频编码使用内置编码器（AAC、PCM 等），无需 LAME"
    echo "   - 已启用 MP4、FLV、MKV 等 muxer"
    echo ""
    echo "✅ 功能说明："
    echo "   - H.264 编码器: avcodec_find_encoder_by_name(\"libx264\")"
    echo "   - AAC 编码器: avcodec_find_encoder_by_name(\"aac\")"
    echo "   - 封装格式支持: mp4, flv, mkv, wav 等"
else
    echo "❌ 编译完成但未生成 libavcodec.so，请检查错误。"
    exit 1
fi
